//
//  CGSuggestionViewController.m
//  SchoolClient
//
//  Created by mac on 15/11/3.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "CGSuggestionViewController.h"

@interface CGSuggestionViewController ()<UITextFieldDelegate,UITextViewDelegate>

@property(nonatomic,strong)CGInputTextView *nameText;          //姓名
@property(nonatomic,strong)CGInputTextView *phoneText;         //手机号
@property(nonatomic,strong)CGInputTextView *qqText;            //QQ
@property(nonatomic,strong)UITextView *suggestText;            //反馈意见
@property(nonatomic,strong)UIButton *submitButton;             //提交
@property(nonatomic,strong)UILabel *placeholderLabel;
@property(nonatomic,strong)NSString *labelText;

@property(nonatomic,strong)UIWindow *coverWindow;
@property(nonatomic,strong)UIView *shadeView;
@property(nonatomic,strong)UIActivityIndicatorView *activity;
@property(nonatomic,strong)UILabel *indicateLabel;

@end

@implementation CGSuggestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Tools getColor:@"ebebeb"];
    self.title = @"意见反馈";
    
    self.labelText = @"请输入您宝贵的意见";
    
    [self layoutUI];
}

-(void)layoutUI{

    //姓名View
    CGRect aFrame = CGRectMake(0, 20, SCREEN_WIDTH, 44);
    self.nameText = [[CGInputTextView alloc]initWithFrame:aFrame];
    self.nameText.label.text = @"姓 名";
    self.nameText.textField.placeholder = @"请输入姓名";
    self.nameText.textField.delegate = self;
    [self.view addSubview:self.nameText];

    //手机号View
    aFrame.origin.y += 45;
    self.phoneText = [[CGInputTextView alloc]initWithFrame:aFrame];
    self.phoneText.label.text = @"手机号";
    self.phoneText.textField.placeholder = @"请输入手机号";
    self.phoneText.textField.delegate = self;
    [self.view addSubview:self.phoneText];
    
    //QQ号View
    aFrame.origin.y += 45;
    self.qqText = [[CGInputTextView alloc]initWithFrame:aFrame];
    self.qqText.label.text = @"QQ 号";
    self.qqText.textField.placeholder = @"请输入QQ号";
    self.qqText.textField.delegate = self;
    [self.view addSubview:self.qqText];
    
    //意见反馈View
    aFrame.origin.y += 65;
    aFrame.size.height = 120;
    self.suggestText = [[UITextView alloc]initWithFrame:aFrame];
    self.suggestText.backgroundColor = [UIColor whiteColor];
    self.suggestText.delegate = self;
    self.suggestText.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:self.suggestText];
    
    CGRect bFrame = CGRectMake(5, 2, SCREEN_WIDTH - 10, 30);
    self.placeholderLabel = [[UILabel alloc]initWithFrame:bFrame];
    self.placeholderLabel.backgroundColor = [UIColor clearColor];
    self.placeholderLabel.textColor = [UIColor grayColor];
    self.placeholderLabel.text = self.labelText;
    [self.suggestText addSubview:self.placeholderLabel];
    
    //提交Button
    aFrame.origin.y += 150;
    aFrame.origin.x = 20;
    aFrame.size.width = SCREEN_WIDTH - 40;
    aFrame.size.height = 44;
    self.submitButton = [[UIButton alloc]initWithFrame:aFrame];
    [self.submitButton setBackgroundImage:[UIImage imageNamed:@"login_btn_bg"] forState:UIControlStateNormal];
    [self.submitButton setTitle:@"提交" forState:UIControlStateNormal];
    self.submitButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [self.view addSubview:self.submitButton];
    //添加点击事件
    [self.submitButton addTarget:self action:@selector(tapSubmitButton) forControlEvents:UIControlEventTouchUpInside];

}


#pragma mark 键盘弹出/退回事件
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.view endEditing:YES];
    return YES;
}
/*
//设置点击空白处时隐藏键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [self.view endEditing:YES];
    
    if (self.suggestText.editable) {
        [UIView animateWithDuration:0.5 animations:^{
            self.view.frame = CGRectMake(0, 63, SCREEN_WIDTH, SCREEN_HEIGHT);
        }];
    }
}
*/

#pragma mark uitextView代理回收键盘
-(void)textViewDidBeginEditing:(UITextView *)textView{
    
    self.placeholderLabel.hidden = YES;
    if (IS_IPHONE4) {
        [UIView animateWithDuration:0.5 animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(0, -120);
        }];

    }else if (IS_IPHONE5){
        [UIView animateWithDuration:0.5 animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(0, -30);
        }];
        
    }else if (IS_IPHONE6){
        [UIView animateWithDuration:0.5 animations:^{
            self.view.transform = CGAffineTransformMakeTranslation(0, -60);
        }];
    }
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissKeyBoard)];
    
}

- (void)dismissKeyBoard {
    
    [self.suggestText resignFirstResponder];
}

- (void)textViewDidEndEditing:(UITextView *)textView{

    self.navigationItem.rightBarButtonItem = nil;
    self.view.transform = CGAffineTransformIdentity;
    
}

#pragma mark 提交按钮点击事件
-(void)tapSubmitButton{
    
    //发送提交请求
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    NSString *suggest = [self.suggestText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    if (suggest.length == 0) {
        [Tools alertWithTitle:@"温馨提示" andMessage:@"亲,请输入您宝贵的意见"];
        return;
    }else{
     parameters[@"content"] = self.suggestText.text;
    }
    
    [self popShadeView];
    
    NSString *name = [self.nameText.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *tphone = [self.phoneText.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *qq = [self.qqText.textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (name.length != 0) {
        parameters[@"name"] = self.nameText.textField.text;
    }
    if (tphone.length != 0) {
         parameters[@"tphone"] = self.phoneText.textField.text;
    }
    if (qq.length != 0) {
        parameters[@"qq"] = self.qqText.textField.text;
    }
   
    
    ZYHRequest *request = [ZYHRequest new];
    [request postAsyncEncryptRequest:@"/main/saveOpinion.app" parameters:parameters success:^(id responseData) {
        
        //字典转模型
        NSString *result = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
        NSLog(@"result:%@",result);
        
        [self hidShadeView];
        
        if ([result isEqualToString:@"1"]) {
            
            [Tools alertWithTitle:@"提交成功" andMessage:@"谢谢亲的宝贵意见！"];
        }

        
    } failure:^(NSError *error) {
        
        [self hidShadeView];
        [Tools alertWithTitle:@"温馨提示" andMessage:@"网络超时,请稍后再试"];
        
    }];


}


#pragma mark 弹出网络延时加载框
-(void)popShadeView{
    
    self.shadeView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.shadeView.backgroundColor = [UIColor blackColor];
    self.shadeView.alpha = 0.3;
    self.coverWindow = [UIApplication sharedApplication].keyWindow;
    [self.coverWindow addSubview:self.shadeView];
    
    //添加一个等待进度轮
    self.activity = [[UIActivityIndicatorView alloc]init];
    self.activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    self.activity.color = [UIColor blueColor];
    CGPoint center = self.shadeView.center;
    self.activity.center = center;
    [self.activity startAnimating];
    [self.coverWindow addSubview:self.activity];
    
    //添加一个提示Label
    self.indicateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.activity.frame)+10, SCREEN_WIDTH, 44)];
    self.indicateLabel.textAlignment = NSTextAlignmentCenter;
    self.indicateLabel.font = [UIFont systemFontOfSize:14];
    self.indicateLabel.textColor = [UIColor blueColor];
    self.indicateLabel.text = @"意见反馈中...";
    [self.coverWindow addSubview:self.indicateLabel];
    
}

#pragma mark 取消网络延时加载框
-(void)hidShadeView{
    
    self.shadeView.hidden = YES;
    self.indicateLabel.hidden = YES;
    [self.activity stopAnimating];
    
}


@end
